const db = uniCloud.database();
const projectDB = db.collection("projects");
const { doSqlMiddle } = require("utils");

async function createProject(data) {
  return doSqlMiddle(async () => {
    let project = await projectDB.add(data);
    return project;
  });
}

async function getProject(id) {
  return doSqlMiddle(async () => {
    const project = await projectDB.doc(id).get();
    const projectData = project.data.length > 0 ? project.data[0] : {};
    return projectData;
  });
}

async function searchProject(params = {}) {
  return doSqlMiddle(async () => {
    const res = await projectDB.where(params).get();
    return res.data || [];
  });
}

async function updateProject(data) {
  return doSqlMiddle(async () => {
    let project = await projectDB.doc(data.id).update(data);
    return project;
  });
}

module.exports = {
  createProject,
  getProject,
  searchProject,
  updateProject
};
